package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.DateUtils;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;
    /**
     * 根据id查询预约信息
     * 姓名，日期，套餐，类型
     */
    public Map findById(Integer id) {
        Map map = orderDao.findById(id);
        if (map != null){

        }
        return null;
    }


    /**
     * 体检预约
     * @param map
     * @return
     */
    @Transactional
    public Result submit(Map map) throws Exception {
//        1. 预约日期的预约人数没有设置的话不能预约。
        String orderDate = (String) map.get("orderDate");//获得预约日期
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
//        2. 预约日期是否已经约满，如果已经约满则无法预约。
        if (orderSetting.getReservations() >= orderSetting.getNumber()){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
//        3. 不能重复预约（同一个用户在同一天预约了同一个套餐）。
        //获取用户的电话号码
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if ( member != null){
            //判断是否重复预约
            Integer memberId = member.getId();//获取会员的id
            Integer  setmealId = (Integer) map.get("setmealId");//获取套餐的id
            Order order = new Order(memberId,date,setmealId);//构造一个order对象
            List<Order> list = orderDao.findByCondition(order);//条件查询
            if (list != null && list.size() > 0){
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else {
//        4. 当前用户不是会员，需要自动完成注册。
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            memberDao.add(member);//自动注册
        }
//        5. 更新已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//会员ID
        order.setOrderDate(date);//预约日期
        order.setOrderStatus(Order.ORDERSTATUS_NO);//预约状态
        order.setOrderType((String) map.get("type"));//预约类型
        order.setSetmealId((Integer) map.get("setmealId"));//套餐id
        orderDao.add(order);

        //增加已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }


}
